package com.gxuwz.eschool;

import static com.google.android.material.internal.ContextUtils.getActivity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentManager;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
//卡充值
public class CardRechargeActivity extends AppCompatActivity {

    private ImageView iv_back;
    private Button btn_10;
    private Button btn_20;
    private Button btn_30;
    private Button btn_selected_amount_view;
    private Button currentSelectedButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_recharge);
        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(MessageForSystem.this, MessageFragment.class);
//                startActivity(intent);
                // 使用SupportFragmentManager来管理Fragment事务
                FragmentManager fragmentManager = getSupportFragmentManager();

                // 检查是否有Fragment可以回退
                if (fragmentManager.getBackStackEntryCount() > 0) {
                    fragmentManager.popBackStack(); // 返回上一个Fragment
                } else {
                    // 如果没有Fragment可以回退，可以结束当前Activity
                    finish();
                }
            }
        });

        btn_10 = findViewById(R.id.btn_10);
        btn_20 = findViewById(R.id.btn_20);
        btn_30 = findViewById(R.id.btn_30);
        btn_selected_amount_view = findViewById(R.id.btn_selected_amount_view);
        btn_selected_amount_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取选中的金额
//                int selectedAmout = Integer.parseInt(btn_selected_amount_view.getText().toString());
                // 跳转到收银台页面并传递数据
                Intent intent = new Intent(CardRechargeActivity.this, CashierActivity.class);
//                intent.putExtra("selected_amount",selectedAmout);
                startActivity(intent);
            }
        });

        setupButtonClickListener(btn_10, "充值10元");
        setupButtonClickListener(btn_20, "充值20元");
        setupButtonClickListener(btn_30, "充值30元");
        currentSelectedButton = btn_10; // 初始化选中按钮
        currentSelectedButton.setSelected(true); // 将第一个按钮设为选中状态
    }
        //更新充值金额按钮的选中状态与对应的颜色
        private void setupButtonClickListener(Button button, final String amount) {
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (currentSelectedButton != null && currentSelectedButton != button) {
                        setSelectedButtonColor(currentSelectedButton, false); // 上一个按钮变灰
                    }
                    setSelectedButtonColor(button, true); // 当前点击的按钮变红
                    currentSelectedButton = button; // 更新当前选中的按钮
                    updateSelectedAmount(amount);
                }
            });
        }



//        btn_10.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                updateSelectedAmount("充值10元");
//            }
//        });
//
//        btn_20.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                updateSelectedAmount("充值20元");
//            }
//        });
//
//        btn_30.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                updateSelectedAmount("充值30元");
//            }
//        });

//    }
    private void setSelectedButtonColor(Button button, boolean isSelected) {
//        int color = isSelected ? Color.RED : Color.GRAY; // 根据isSelected选择颜色
        int colorResId = isSelected ? R.color.red : R.color.color_gray;
        int color = ContextCompat.getColor(this, colorResId);
        button.setBackgroundColor(color);
    }
    private void updateSelectedAmount(String amount) {

//        currentSelectedButton.setBackgroundColor(getResources().getColor(R.color.red));
        btn_selected_amount_view.setBackgroundColor(getResources().getColor(R.color.red));
        btn_selected_amount_view.setPadding(0, 0, 0, 0);
        btn_selected_amount_view.setText(String.valueOf(amount));
    }
}
